package com.hdmall.business.common.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class PhoneUtil {
    /**
     *  测试手机号码是来自哪个城市的，利用拍拍网的API
     * @param mobileNumber
     * @return
     */
    public static String calcMobileCity( String mobileNumber){
        //获取拍拍网的API地址
        String urlString = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel="+mobileNumber;
        StringBuffer sb = new StringBuffer();
        BufferedReader buffer;
        try {
            URL url = new URL(urlString);
            //获取URL地址中的页面内容
            InputStream in = url.openStream();
            // 解决乱码问题
            buffer = new BufferedReader(new InputStreamReader(in,"gb2312"));
            String line = null;
            //一行一行的读取数据
            while((line = buffer.readLine()) != null){
                sb.append(line);
            }
            in.close();
            buffer.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();

    }

    public static void main(String[] args) {
        String city=calcMobileCity("18358912521");
        System.out.println(city);
    }
}
